/**
 *
 */

package com.moogene.saad.gameplay.defender;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import android.graphics.Canvas;

import com.moogene.android.R;
import com.moogene.android.engine.animlite.Animlite;
import com.moogene.android.engine.mmi.Layer;
import com.moogene.saad.gameplay.bullet.Bullet;
import com.moogene.saad.gameplay.bullet.Effectable;
import com.moogene.saad.gameplay.bullet.Physics;
import com.moogene.saad.gameplay.data.IDataHandler;

/**
 * @author 28849218
 */
public class TowerTemplate extends Layer implements IDataHandler{
//    // TODO width and height should be adjust according the screen size.
//    private final static int WIDTH = 100;
//
//    private final static int HEIGHT = 100;
	
	private Bullet bulletTemplate;

//    private Animlite bulletAnim;
//
//    private Effectable effectable;
//
    final private DefAction action;
//
    private final int range;

    private final int frequency;
    
    private TowerTemplate(DefAction action, int range, int frequency, Bullet bullet) {
        super(action.getAnimlite().getFrameWidth(), action.getAnimlite().getFrameHeight());
        this.action = action;
        this.range = range;
        this.frequency = frequency;
        this.bulletTemplate = bullet;
    }

    public static HashMap<String, TowerTemplate> load(String name){
    	HashMap<String, TowerTemplate> towerTemplates = new HashMap<String, TowerTemplate>();
    	Physics.load(name);
    	if(name.equalsIgnoreCase("level1")){
    		int range = 12;
    		int frequency = 1;
    		String effectType = "HurtEffect";
    		String actionName = "actMouse1";
    		int effectPoint = 5;
    		Animlite bulletAnim = null;
    		Constructor<?> constructor = null;
        	Effectable effectable = null;
    		try {
    			constructor = Class.forName("com.moogene.saad.gameplay.bullet." + effectType).getConstructor(new Class[]{Integer.TYPE});
    		} catch (SecurityException e) {
    			e.printStackTrace();
    		} catch (NoSuchMethodException e) {
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    		try {
    			if(constructor != null){
    				effectable = (Effectable) constructor.newInstance(effectPoint);
    			}
    		} catch (IllegalArgumentException e) {
    			e.printStackTrace();
    		} catch (InstantiationException e) {
    			e.printStackTrace();
    		} catch (IllegalAccessException e) {
    			e.printStackTrace();
    		} catch (InvocationTargetException e) {
    			e.printStackTrace();
    		}
    		boolean isGuilded = false;
    		Bullet b = new Bullet(effectable, null);
    		TowerTemplate t = new TowerTemplate(DefAction.load(actionName), range, frequency, b);
    		towerTemplates.put("mouse1", t);
    		range = 15;
    		frequency = 2;
    		bulletAnim = new Animlite(R.drawable.bullet_flysword);
    		bulletAnim.setAnchor(138, 20);
    		b = new Bullet(effectable, bulletAnim);
    		t = new TowerTemplate(DefAction.load("actMouse2"), range, frequency, b);
    		towerTemplates.put("mouse2", t);
    	}
    	return towerTemplates;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.moogene.android.engine.mmi.Layer#draw(android.graphics.Canvas)
     */
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        action.draw(canvas, x, y);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.moogene.android.engine.mmi.Layer#update()
     */
    @Override
    public void update() {
    	super.update();
    }

    @Override
    protected boolean canCopy() {
        return true;
    }
    
    @Override
    protected Animlite getAttachedAnimlite() {
        return action.getAnimlite();
    }
    
    public Bullet createBullet(){
    	return (Bullet) bulletTemplate.clone();
    }

    /**
     * @return the range
     */
    public int getRange() {
        return range;
    }

    /**
     * @return the frequency
     */
    public int getFrequency() {
        return frequency;
    }

	@Override
	public void checkVersion() {
		// TODO Auto-generated method stub

	}

	@Override
	public void download() {
		// TODO Auto-generated method stub

	}

	@Override
	public void save() {
		// TODO Auto-generated method stub

	}

	@Override
	public void upload() {
		// TODO Auto-generated method stub

	}

	DefAction getAction(){
		return action;
	}

	@Override
	public Tower copy(){
		copiedLayer = new Tower(this);
		return (Tower)copiedLayer;
	}

}
